package com.fz.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fz.entity.Book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by webrx on 2017-09-07.
 */
@Controller
public class Test {
//验证配置了json之后将数组，字符串和对象转换为json格式
//如果没有配置json，下面这三种写法就会报错


    @RequestMapping("/json") @ResponseBody
    public Object ttt(){
        return new String[]{"java","html","css","js"};
        //显示结果  ["java","html","css","js"]
    }

    @RequestMapping("/bk") @ResponseBody
    public Book bk(){
        Book b=new Book();
        b.setName("于壮壮");
        b.setPrice(250);
        b.setId(10);
        b.setBdate(new Date());
        return b;
        //显示结果：{"id":10,"name":"于壮壮","price":250.0,"bdate":1504782999834}
        //其中1504782999834是将时间转换成的，也可以通过一个例子，把这个数字再转为时间格式
        /*@Test
        public void abc(){
            Date d=new Date();
            //转换时由于1504782999834超出Int范围，所以要加个l转换成long类型的
            d.setTime(1504782999834l);
            System.out.println(d);    //结果是：Thu Sep 07 19:16:39 CST 2017
            //也可以再转换成时间格式
            System.out.println(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(d));
            //2017-09-07 07:16:39
        }*/


        //关于这个数字也可以在html网页验证一下，例如，my.html



    }

        //也可以弄个集合，里面装一群对象
    @RequestMapping("/bbk")    @ResponseBody
    public List<Book> bks(){
    List<Book> bks=new ArrayList<Book>();
        Book b=new Book();
        b.setName("于壮壮");
        b.setPrice(250);
        b.setId(10);
        b.setBdate(new Date());
        Book b1=new Book();
        b1.setName("壮");
        b1.setPrice(200);
        b1.setId(11);
        b1.setBdate(new Date());
        Book b2=new Book();
        b2.setName("壮壮");
        b2.setPrice(299);
        b2.setId(12);
        b2.setBdate(new Date());
        bks.add(b);
        bks.add(b1);
        bks.add(b2);
       return bks;

       //结果
        //[{"id":10,"name":"于壮壮","price":250.0,"bdate":1504784653932},{"id":11,"name":"壮","price":200.0,"bdate":1504784653932},{"id":12,"name":"壮壮","price":299.0,"bdate":1504784653932}]

    }

//下面是使用了阿里巴巴json的方法，推荐以后多使用这种转换json格式，这种的话返回值只能是String，上面的那种方法返回值可以是多种，集合，对象，数组什么的

    @RequestMapping("/bbbk")    @ResponseBody
    public String bkss(){
        List<Book> bkss=new ArrayList<Book>();
        Book b=new Book();
        b.setName("于壮壮");
        b.setPrice(250);
        b.setId(10);
        b.setBdate(new Date());
        Book b1=new Book();
        b1.setName("壮");
        b1.setPrice(200);
        b1.setId(11);
        b1.setBdate(new Date());
        Book b2=new Book();
        b2.setName("壮壮");
        b2.setPrice(299);
        b2.setId(12);
        b2.setBdate(new Date());
        bkss.add(b);
        bkss.add(b1);
        bkss.add(b2);
        return JSONObject.toJSONString(bkss);
    //[{"bdate":1504785625226,"id":10,"name":"于壮壮","price":250.0},{"bdate":1504785625226,"id":11,"name":"壮","price":200.0},{"bdate":1504785625226,"id":12,"name":"壮壮","price":299.0}]
    }

    @RequestMapping("/jsonn") @ResponseBody
    public String tttt(){
        String[] s=new String[]{"java","html","css","js"};
        return JSONObject.toJSONString(s);
        //显示结果  ["java","html","css","js"]
    }

    @RequestMapping("/bkk") @ResponseBody
    public String bkk() {
        Book bb = new Book();
        bb.setName("于壮壮");
        bb.setPrice(250);
        bb.setId(10);
        bb.setBdate(new Date());
        return JSONObject.toJSONString(bb);
        //{"bdate":1504786032678,"id":10,"name":"于壮壮","price":250.0}
    }




    //在my.html网页上作出时钟的效果,运用ajax 的技术
    @RequestMapping(value = "/timerrrr",method = RequestMethod.POST) @ResponseBody
    public String timer(){
        return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
    }


}
